﻿/*
 * My Timer - frmMain.cs
 * 
 * Copyright (C) 2009 Ahmad Hanis Azmi
 * 
 * This file is part of My Timer
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace My_Timer
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            //this.lvTimers.ItemChecked += new ItemCheckedEventHandler(this.lvTimers_ItemChecked);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            LoadString.f_LoadStringMain();
            GlobalClass.r_frmMain.AcceptButton = GlobalClass.r_frmMain.btnOK;

            // reload data from r_Timers to lvTimers

            if (GlobalClass.r_Timers.Count > 0)
            {
                List<int> i_Tag = new List<int>(GlobalClass.r_Timers.Keys);
                foreach (int i in i_Tag)
                {
                    string s = "";

                    TimersArray t_TimersArray = GlobalClass.r_Timers[i];

                    s = Formatting.Formatting.formatDaysHoursMinutesSecondsLong(t_TimersArray.tDays, 
                        t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds);

                    switch (t_TimersArray.timerState)
                    {
                        case 0:
                            GlobalClass.r_frmMain.lvTimers.Items.Add(i.ToString(), t_TimersArray.Name, null).SubItems.Add(s);

                            break;

                        case 1:
                            GlobalClass.r_frmMain.lvTimers.Items.Add(i.ToString(), t_TimersArray.Name + " (Running)", null).SubItems.Add(s);

                            break;

                        case 2:
                            GlobalClass.r_frmMain.lvTimers.Items.Add(i.ToString(), t_TimersArray.Name + " (Paused)", null).SubItems.Add(s);

                            break;
                    }

                    t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(i.ToString());
                    GlobalClass.r_frmMain.lvTimers.Items[t_TimersArray.iIndexOfKey].Tag = i;
                    int i_IndexOfKey = t_TimersArray.iIndexOfKey;

                    GlobalClass.r_Timers[i] = t_TimersArray;

                    switch (t_TimersArray.IsFormEnable)
                    {
                        case true:
                            GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].Font =
                                new Font("Microsoft Sans Serif", (float)8.25, FontStyle.Bold);

                            break;

                        case false:

                            break;
                    }
                }
            }
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            GlobalClass.f_chkfrmAbout();
            //GlobalClass.r_frmAbout.Visible = false;

            if (GlobalClass.r_frmAbout.Visible == false)
            {
                GlobalClass.r_frmAbout.Hide();
                GlobalClass.r_frmAbout.ShowDialog(GlobalClass.r_frmMain); 
            }

            GlobalClass.r_frmAbout.Activate();
        }

        private void btnGoToTimers_Click(object sender, EventArgs e)
        {

        }

        private void btnTimersAdd_Click(object sender, EventArgs e)
        {
            GlobalClass.frmTimersStatus = "Add";
            GlobalClass.f_chkfrmTimers();

            if (GlobalClass.r_frmTimers.Visible == false)
            { 
                GlobalClass.r_frmTimers.ShowDialog(GlobalClass.r_frmMain); 
            }

            GlobalClass.r_frmTimers.Activate();

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void btnTimersEdit_Click(object sender, EventArgs e)
        {
            //System.Windows.Forms.MessageBox.Show(this.lvTimers.SelectedIndices.Count.ToString());

            if (this.lvTimers.SelectedIndices.Count == 1)
            {
                int i_Tag = (int)lvTimers.Items[lvTimers.SelectedIndices[0]].Tag;

                GlobalClass.frmTimersStatus = "Edit";
                GlobalClass.f_chkfrmTimers();

                TimersArray t_TimersArray = GlobalClass.r_Timers[i_Tag];

                GlobalClass.r_frmTimers.Tag = i_Tag;
                GlobalClass.r_frmTimers.textBoxTimerTitle.Text = t_TimersArray.Name;
                GlobalClass.r_frmTimers.comboBoxOnTimerEnd.SelectedIndex = t_TimersArray.OnTimerEnd;
                GlobalClass.r_frmTimers.checkBoxOnTimerEndPlayAudio.Checked = t_TimersArray.PlayAudio;

                frmTimers.AudioPath.Path = t_TimersArray.PlayAudioPath;

                GlobalClass.r_frmTimers.buttonBrowseAudio.Text = t_TimersArray.PlayAudioPath;
                GlobalClass.r_frmTimers.checkBoxOnTimerEndShowMsgbox.Checked = t_TimersArray.PopupMsgBox;
                GlobalClass.r_frmTimers.textBoxOnTimerEndMsgbox.Text = t_TimersArray.MsgBoxString;
                GlobalClass.r_frmTimers.numericUpDownDays.Value = (decimal)t_TimersArray.Days;
                GlobalClass.r_frmTimers.numericUpDownHours.Value = (decimal)t_TimersArray.Hours;
                GlobalClass.r_frmTimers.numericUpDownMinutes.Value = (decimal)t_TimersArray.Minutes;
                GlobalClass.r_frmTimers.numericUpDownSeconds.Value = (decimal)t_TimersArray.Seconds;
                GlobalClass.r_frmTimers.checkBoxShutdownComputer.CheckState = CheckState.Unchecked;
                GlobalClass.r_frmTimers.checkBoxRestartComputer.CheckState = CheckState.Unchecked;

                switch (t_TimersArray.ShutdownComputer)
                {
                    case 1:
                        GlobalClass.r_frmTimers.checkBoxShutdownComputer.CheckState = CheckState.Checked;

                        break;

                    case 2:
                        GlobalClass.r_frmTimers.checkBoxRestartComputer.CheckState = CheckState.Checked;

                        break;
                }

                if (t_TimersArray.openApp == true)
                {
                    GlobalClass.r_frmTimers.checkBoxRun.CheckState = CheckState.Checked;
                    GlobalClass.r_frmTimers.textBoxRun.Text = t_TimersArray.appLocation;
                }

                if (GlobalClass.r_frmTimers.Visible == false)
                {
                    GlobalClass.r_frmTimers.ShowDialog(GlobalClass.r_frmMain);
                }

                GlobalClass.r_frmTimers.Activate();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void btnTimersDelete_Click(object sender, EventArgs e)
        {
            if (this.lvTimers.SelectedIndices.Count == 1)
            {
                int i = lvTimers.SelectedIndices[0];
                int i_Tag = (int)lvTimers.Items[i].Tag;
                
                TimersArray t_TimersArray = GlobalClass.r_Timers[i_Tag];

                t_TimersArray.timerDeleted = true;

                if (t_TimersArray.TimerForm != null)
                {
                    if (((t_TimersArray.TimerForm.Created == true)) || (t_TimersArray.TimerForm.IsDisposed == false))
                    {
                        t_TimersArray.TimerForm.timerOpacityControl.Enabled = false;
                        t_TimersArray.TimerForm.Close();
                        t_TimersArray.TimerForm.Dispose();
                    }
                }

                GlobalClass.r_Timers[i_Tag] = t_TimersArray;
                GlobalClass.r_Timers.Remove(i_Tag); //recheck
                this.lvTimers.Items[i].Remove();
            }
        }

        private void btnTimersCopy_Click(object sender, EventArgs e)
        {
            if (this.lvTimers.SelectedIndices.Count == 1)
            {
                //Start: New code
                int i = lvTimers.SelectedIndices[0];
                int i_Tag = (int)lvTimers.Items[i].Tag;
                string s = "";

                TimersArray t_TimersArray = GlobalClass.r_Timers[i_Tag];

                t_TimersArray.TimerControl = null;
                t_TimersArray.TimerForm = null;
                t_TimersArray.IsFormEnable = false;
                //t_TimersArray.r_PlayAudio = null;
                t_TimersArray.tKey = GlobalClass.nextKey;
                t_TimersArray.tDays = t_TimersArray.Days;
                t_TimersArray.tHours = t_TimersArray.Hours;
                t_TimersArray.tMinutes = t_TimersArray.Minutes;
                t_TimersArray.tSeconds = t_TimersArray.Seconds;

                s = Formatting.Formatting.formatDaysHoursMinutesSecondsLong(t_TimersArray.Days, t_TimersArray.Hours, 
                    t_TimersArray.Minutes, t_TimersArray.Seconds);

                GlobalClass.r_frmMain.lvTimers.Items.Add(GlobalClass.nextKey.ToString(), t_TimersArray.Name, null)
                    .SubItems.Add(s);

                t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(
                    GlobalClass.nextKey.ToString());

                GlobalClass.r_frmMain.lvTimers.Items[t_TimersArray.iIndexOfKey].Tag = GlobalClass.nextKey;
                GlobalClass.r_Timers.Add(GlobalClass.nextKey, t_TimersArray);
                GlobalClass.nextKey++;

                //End: New code
            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            if ((GlobalClass.r_frmSettings == null) || (GlobalClass.r_frmSettings.Created == false) || (GlobalClass.r_frmSettings.Visible == false))
            {
                GlobalClass.r_frmSettings = new frmSettings();
            }

            GlobalClass.r_frmSettings.ShowDialog(this);
        }

        private void buttonEnableTimer_Click(object sender, EventArgs e)
        {
            if (this.lvTimers.SelectedIndices.Count == 1)
            {
                int i = this.lvTimers.SelectedIndices[0];
                int i_Tag = (int)lvTimers.Items[i].Tag;

                TimersArray t_TimersArray;
                t_TimersArray = GlobalClass.r_Timers[i_Tag];

                if (t_TimersArray.IsFormEnable == false)
                {
                    string s = "";

                    s = Formatting.Formatting.formatDaysHoursMinutesSecondsShort(t_TimersArray.tDays, 
                        t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds);

                    frmTimersPopup r_frmTimersPopup = new frmTimersPopup();
                    
                    r_frmTimersPopup.labelTimer.Text = s;
                    r_frmTimersPopup.Tag = i_Tag;

                    // frmTimersPopup size
                    r_frmTimersPopup.Size = new Size(t_TimersArray.xSize, t_TimersArray.ySize);

                    r_frmTimersPopup.Show();

                    // frmTimersPopup location
                    r_frmTimersPopup.Location = new Point(t_TimersArray.xPossition, t_TimersArray.yPossition);

                    // frmTimersPopup Opacity
                    r_frmTimersPopup.Opacity = t_TimersArray.form_Opacity;

                    // frmTimersPopup TopMost?
                    r_frmTimersPopup.TopMost = t_TimersArray.form_AlwaysOnTop;

                    //this.lvTimers.Items[i].Checked = true;

                    this.lvTimers.Items[i].Font = new Font("Microsoft Sans Serif", (float)8.25, FontStyle.Bold);
                    t_TimersArray.IsFormEnable = true;

                    t_TimersArray.TimerForm = r_frmTimersPopup;
                }

                GlobalClass.r_Timers[i_Tag] = t_TimersArray;
            }
        }

        private void buttonHideTimer_Click(object sender, EventArgs e)
        {
            if (this.lvTimers.SelectedIndices.Count == 1)
            {
                int i = this.lvTimers.SelectedIndices[0];
                int i_Tag = (int)lvTimers.Items[i].Tag;

                TimersArray t_TimersArray = GlobalClass.r_Timers[i_Tag];

                if (t_TimersArray.IsFormEnable == true)
                {
                    t_TimersArray.IsFormEnable = false;
                    t_TimersArray.TimerForm.Close();
                    t_TimersArray.TimerForm.Dispose();
                    t_TimersArray.TimerForm = null;
                }

                GlobalClass.r_Timers[i_Tag] = t_TimersArray;
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            SaveSetting.SaveTimerSetting();
        }

        private void pictureBoxStartTimer_Click(object sender, EventArgs e)
        {
            // see pictureBoxStartTimer_MouseUp
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Program.r_frmTray.Close();
            Program.ExitApplication();
        }

        private void pictureBoxPauseTimer_Click(object sender, EventArgs e)
        {
            // see pictureBoxPauseTimer_MouseUp
        }

        private void pictureBoxStopTimer_Click(object sender, EventArgs e)
        {
            // see pictureBoxStopTimer_MouseUp
        }

        private void pictureBoxStartTimer_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pictureBoxStartTimer.Location = new Point(pictureBoxStartTimer.Location.X + 1, pictureBoxStartTimer.Location.Y + 1);
            }
        }

        private void pictureBoxStartTimer_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pictureBoxStartTimer.Location = new Point(pictureBoxStartTimer.Location.X - 1, pictureBoxStartTimer.Location.Y - 1);

                if (lvTimers.SelectedIndices.Count == 1)
                {
                    int i_Tag = (int)lvTimers.Items[lvTimers.SelectedIndices[0]].Tag;

                    TimerControl.StartTimer(i_Tag);
                }
            }
        }

        private void pictureBoxPauseTimer_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pictureBoxPauseTimer.Location = new Point(pictureBoxPauseTimer.Location.X + 1, pictureBoxPauseTimer.Location.Y + 1);
            }
        }

        private void pictureBoxPauseTimer_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pictureBoxPauseTimer.Location = new Point(pictureBoxPauseTimer.Location.X - 1, pictureBoxPauseTimer.Location.Y - 1);

                if (lvTimers.SelectedIndices.Count == 1)
                {
                    int i_Tag = (int)lvTimers.Items[lvTimers.SelectedIndices[0]].Tag;

                    TimerControl.PauseTimer(i_Tag);
                }
            }
        }

        private void pictureBoxStopTimer_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pictureBoxStopTimer.Location = new Point(pictureBoxStopTimer.Location.X + 1, pictureBoxStopTimer.Location.Y + 1);
            }
        }

        private void pictureBoxStopTimer_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pictureBoxStopTimer.Location = new Point(pictureBoxStopTimer.Location.X - 1, pictureBoxStopTimer.Location.Y - 1);

                frmTray.doPlayAudio(1, null);

                if (lvTimers.SelectedIndices.Count == 1)
                {
                    int i_Tag = (int)lvTimers.Items[lvTimers.SelectedIndices[0]].Tag;

                    TimerControl.StopTimer(i_Tag);
                }
            }
        }

        /// <summary>
        /// Bring up dialog to add new alarm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAddAlarm_Click(object sender, EventArgs e)
        {
            Alarm.addOrEdit = 0;
            FormAddEditAlarm formAddEditAlarm = new FormAddEditAlarm();

            formAddEditAlarm.Text = "Add new alarm";
            formAddEditAlarm.comboBoxTimeValue.SelectedIndex = 0;
            formAddEditAlarm.checkBoxAlarmDayAll.Checked = true;
            formAddEditAlarm.checkBoxAlarmDaySunday.Checked = false;
            formAddEditAlarm.checkBoxAlarmDayMonday.Checked = false;
            formAddEditAlarm.checkBoxAlarmDayTuesday.Checked = false;
            formAddEditAlarm.checkBoxAlarmDayWednesday.Checked = false;
            formAddEditAlarm.checkBoxAlarmDayThursday.Checked = false;
            formAddEditAlarm.checkBoxAlarmDayFriday.Checked = false;
            formAddEditAlarm.checkBoxAlarmDaySaturday.Checked = false;

            formAddEditAlarm.checkBoxAlarmDaySunday.Enabled = false;
            formAddEditAlarm.checkBoxAlarmDayMonday.Enabled = false;
            formAddEditAlarm.checkBoxAlarmDayTuesday.Enabled = false;
            formAddEditAlarm.checkBoxAlarmDayWednesday.Enabled = false;
            formAddEditAlarm.checkBoxAlarmDayThursday.Enabled = false;
            formAddEditAlarm.checkBoxAlarmDayFriday.Enabled = false;
            formAddEditAlarm.checkBoxAlarmDaySaturday.Enabled = false;

            formAddEditAlarm.ShowDialog(this);
        }

        /// <summary>
        /// Bring up the dialog to edit alarm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEditAlarm_Click(object sender, EventArgs e)
        {
            if (this.listViewAlarms.SelectedIndices.Count == 1)
            {
                Alarm.addOrEdit = 1;
                FormAddEditAlarm frmAddEditAlarm = new FormAddEditAlarm();

                int iTag = (int)listViewAlarms.SelectedItems[0].Tag;
                AlarmType alarm = Alarm.alarmList[iTag];

                frmAddEditAlarm.Tag = iTag;
                frmAddEditAlarm.textBoxAlarmTitle.Text = alarm.name;

                //todo: dev starts here: fill in value to dialog

                frmAddEditAlarm.ShowDialog(this);

            }
        }
 
    }
}
